import os, re, time
from common.read_ini import Dispose_ini
from common.Scheduled_Tasks import Scheduled_Tasks
class Flag_Detection():
    def __init__(self):
        self.a_path = '/home/A.log'
        self.b_path = '/home/B.log'
        self.c_path = '/home/C.log'
        self.Desktop_path = os.popen("find / -name Desktop").read().strip()
        self.linux_ft_path=os.popen("find / -name linux_ft_script |tail -1").read().strip()+"/main.sh"
        self.over_path = os.popen("find / -name over_night |tail -1").read().strip()+"/over_night.sh"
        self.TF_path =os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
        self.stask=Scheduled_Tasks()
    def flag_check(self):
        #检测标志位，传入路径
        num_list = []
        if os.path.exists(self.a_path):
            num_list.append(1)
        if os.path.exists(self.b_path):
            num_list.append(2)
        if os.path.exists(self.c_path):
            num_list.append(3)

        return num_list

    def run(self):
        num_list = tuple(self.flag_check())
        if num_list == (1,):
            # 运行老化
            os.system(self.over_path)
            print('Case 1')
            #删除前测log日志
            FT_path = os.popen("find / -name FT").read().strip()
            os.system(f"rm -rf {FT_path}")
            log_path = self.TF_path+"/log"
            os.system(f"rm -rf {log_path}")
            time.sleep(1)
        elif num_list == (1, 2):
            sn = input("Please scan the SN QR code:")
            # 运行后测试
            os.system(f'echo {sn}|{self.linux_ft_path}')
            print('Case 2')
        elif num_list == (1, 2, 3):
            # 什么都不做,删除定时任务，删除产测、老化脚本
            user_path = os.path.dirname(self.Desktop_path)
            ND_path = user_path+"/ND_Test"
            self.stask.delet_task()
            cmd = self.Desktop_path+"/*"
            os.system(f"rm -rf {cmd}")
            os.system(f"rm -rf {ND_path}")
            print(f'Case 3')
        elif num_list == (1,3):
            os.system(self.over_path)
            print('Case 1')
        else:
            sn = input("Please scan the SN QR code:")
            # 运行前测
            os.system(f'echo {sn}|{self.linux_ft_path}')
            print('Case 4')



if __name__ == '__main__':
    f=Flag_Detection()
    f.run()